<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" media="screen" href="../../css/dataTables.bootstrap.min.css">
<link rel="stylesheet" href="../../layui/css/layui.css" media="all" />
</head>
<body>
    <div>
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <div>
                <header style="height: 100%">
					 <div align="left">
                        <table style="width: 100%">
                            <tr>
                                <td>
                                    <button type="button" class="layui-btn" id="test1">
									  <i class="layui-icon">&#xe67c;</i>上传文件
									</button>
                                </td>
                                <td align="right">
                                    <form class="form-inline" onsubmit="return false">
                                        <div class="form-group">
                                           <button id="searchBt" class="layui-btn layui-btn-sm" permission="sys:file:query"><i class="layui-icon">&#xe615;</i>搜索</button>
                                        </div>
                                    </form>
                                </td>
                            </tr> 
                        </table>
                    </div>
                </header>
                
                <div>
                    <div class="widget-body no-padding">
                        <table id="dt-table" class="table table-striped table-bordered table-hover" style="width:100%">
                            <thead>
                                <tr>
                                </tr>                       
                                <tr>
                                    <th>url</th>
                                    <th>path</th>
                                    <th>contentType</th>
                                    <th>修改时间</th>
                                    <th>管理</th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

<script type="text/javascript" src="../../js/libs/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="../../js/jq.js"></script>
<script type="text/javascript" src="../../layui/layui.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="../../js/my/permission.js"></script>
<script type="text/javascript">
var pers = checkPermission();

var pro = window.location.protocol;
var host = window.location.host;
var domain = pro + "//" + host;

var example;
function init(){
	example = 
    	$('#dt-table').DataTable({
        	"searching": false,
        	"processing": false,
        	"serverSide" : true,
        	"language": {
                "url": "/js/plugin/datatables/Chinese.lang"
            },
        	"ajax": {
        		"url" : "/files",
        		"type":"get",
        		"data":function(d){
    			
        		},
    			"error":function(xhr, textStatus, errorThrown){
    				var msg = xhr.responseText;
    				console.log(msg);
    				var response = JSON.parse(msg);
    				var code = response.code;
    				var message = response.message;
    				if (code == 400) {
    					layer.msg(message);
    				} else if (code == 401) {
    					localStorage.removeItem("token");
    					layer.msg("token过期，请先登录", {shift: -1, time: 1000}, function(){
    						location.href = '/login.html';
                        });
    				} else if (code == 403) {
    					console.log("未授权:" + message);
    					layer.msg('未授权');
    				} else if (code == 500) {
    					layer.msg('系统错误：' + message);
    				}
    			}
        	},
        	"dom": "<'dt-toolbar'r>t<'dt-toolbar-footer'<'col-sm-10 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-10' p v>>",	
            "columns": [
                { 
                	"data": "url", 
                	"defaultContent": "",
                	"render": function (data, type, row) {
                		var url = row['url'];
                		var type = row['type'];
                		var u = "";
                		if(type == 1) {
                			var src = domain + "/statics" + url;
                			u = "<a target='_blank' href='" + src +"'><img width='30' src='" + src + "'></img></a>";
                		} else {
                			u = url;
                		}
                    	return u;
                  	}
                },
                { "data": "path", "defaultContent": ""},
                { "data": "contentType", "defaultContent": ""},
                { "data": "updateTime", "defaultContent": ""},
                { 
                	"data": "", 
                	"defaultContent": "",
                	"orderable":false,
                	"render": function (data, type, row) {
                		var id = row['id'];
                        var del = buttonDel(id, "sys:file:del", pers);
                        return del;
                  	}
                },
                
            ],
           "order": [[ 3, "desc" ]]
        } );
}

function del(id){
	layer.confirm('确定要删除吗？', {
        btn : [ '确定', '取消' ]
    }, function() {
    	$.ajax({
            type : 'delete',
            url : '/files/'+id,
            success : function(data) {
                example.ajax.reload();
                layer.msg("删除成功");
            }
        });
        
        layer.close(1);
    });
}

$("#searchBt").click(function(){
	example.ajax.reload();
});

layui.use('upload', function(){
	var upload = layui.upload;
	upload.render({
	    elem: '#test1' //绑定元素
	    ,accept: 'file' //允许上传的文件类型
	    ,url: '/files' //上传接口
	    ,done: function(res, index, upload){
	    	layer.msg("上传成功");
		 	example.ajax.reload();
	    }
	  });
});

init();
</script>